package com.anycc.cloud.core.controller;

import com.anycc.cloud.core.dto.Breadcrumb;
import com.anycc.cloud.core.dto.Menu;
import com.anycc.cloud.core.interceptor.UserContextHolder;
import com.anycc.cloud.core.service.AccountService;
import com.anycc.cloud.core.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * menu controller
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private AccountService accountService;

    /**
     * make menus
     *
     * @param selectedSN selectedSN
     * @return List
     */
    @GetMapping("makeMenus")
    public List<Menu> makeMenus(@RequestParam("selectedSN") String selectedSN) {
        String username = UserContextHolder.getContext().getUsername();
        if (username != null) {
            return menuService.makeMenu(username,accountService.findByUsername(username), selectedSN);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * make menus
     *
     * @param selectedSN selectedSN
     * @return List
     */
    @GetMapping("makeBreadcrumb")
    public List<Breadcrumb> makeBreadcrumb(@RequestParam("selectedSN") String selectedSN) {
        return menuService.makeBreadcrumb(selectedSN);
    }

    /**
     * 根据角色名获取完整权限字符串
     *
     * @param roleNames 角色名称
     * @return 权限字符串集合
     */
    @PostMapping("findFullPermissionString")
    public List<String> findFullPermissionString(@RequestParam("roleNames") List<String> roleNames) {
        return menuService.findFullPermissionString(roleNames);
    }


}
